package com.example.qxglxt.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.example.qxglxt.dao.IUsersDao;
import com.example.qxglxt.dto.UsersDTO;
import com.example.qxglxt.model.Users;
import com.example.qxglxt.service.IUsersService;

import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody; 
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiImplicitParam;
//import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation; 

@RestController
@RequestMapping("/users")
public class UsersController {

    @Resource
    private IUsersDao usersDao;
    @Resource
    private IUsersService usersService; 
    
    @ApiOperation(value = "查询所有",notes = "查询所有")
    @PostMapping("/findAll")
    public Map<String,Object> findAll(){
        List<Users> list = usersDao.findAll();
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code", 0);
        map.put("msg", "查询成功");
        map.put("count", list.size());
        map.put("data",list);
        return map;
    }

    @ApiOperation(value = "多条件分页查询用户",notes = "多条件分页查询用户")
    @GetMapping(value = "/findPage")
    public Map<String ,Object> findPage(@RequestBody UsersDTO dto){
        Page<Users> page=usersService.findAllPage(dto);
     Map<String ,Object> map=new HashMap<String,Object>();
    map.put("code", 0);
         map.put("msg", "查询成功");
         map.put("count", page.getNumberOfElements());
        map.put("data",page.getContent());
         return map;
     }  
     @ApiOperation(value = "添加用户信息",notes = "添加/修改用户信息")
     @PostMapping(value = "/addUser")
     public Users addUsers(@RequestBody Users u){
         return usersService.saveUsers(u);
     }
     @ApiOperation(value = "删除用户信息",notes = "删除用户信息")
     @ApiImplicitParam(name = "id",value = "编号",required = true,paramType = "path")
     @DeleteMapping(value = "/{id}")
     public void delUserById(@PathVariable Integer id){
         usersService.delUsersById(id);
     }  
    @ApiOperation(value = "是否锁定用户",notes = "是否锁定")
    /* @ApiImplicitParams({
        @ApiImplicitParam(value = "id",name = "用户编号",required = true,paramType = "path"),
        @ApiImplicitParam(value = "IsLockout",name = "是/否",required = true,paramType = "path")
    }) */
    @PostMapping("/Islockon")
    public int Islockon(@RequestParam("LoginName") String LoginName,@RequestParam("IsLockout") String IsLockout){
        return usersDao.editIslockout(LoginName,IsLockout);
    }

    @ApiOperation(value = "重置密码",notes = "重置密码")
   /*  @ApiImplicitParams({
        @ApiImplicitParam(value = "id",name = "用户编号",required = true,paramType = "path"),
        @ApiImplicitParam(value = "password",name = "密码",required = true,paramType = "path")
    }) */
    @Transactional
    @PutMapping("/editpwd/{id}/{PassWord}")
    public int editpwd(@PathVariable Integer id, @PathVariable String PassWord){
        return usersDao.editpwd(id, PassWord);
    }   

    @ApiOperation(value = "登录",notes = "登录")
    @Transactional
    @GetMapping("/login/{LoginName}/{PassWord}")
    public Users login(@PathVariable String LoginName,@PathVariable String PassWord){
        return usersDao.login(LoginName, PassWord);
    }

}